package com.kai.controller;


import com.kai.pojo.Result;
import com.kai.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator  aliyunOSSOperator;


    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {

//        log.info("文件上传接收参数>: {},{},{}",name,age,file);
//
//        String originalFilename = file.getOriginalFilename();
//        String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFileName = UUID.randomUUID().toString() + extention;
//
//        file.transferTo(new File("D:/images/" + newFileName));
//        return Result.success();

        log.info("文件上传接收参数>: {}",file.getOriginalFilename());

        //上传到阿里云OOS 返回url地址
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        return Result.success(url);
    }

}
